package com.kfm.base.oop4;

public class Demo02 {

    public static void main(String[] args) {
        Graphics graphics = new Triangle();
        graphics.draw();

        // 向下转型, 强制类型转换
        Triangle triangle = (Triangle) graphics;
//        Triangle triangle = (Triangle) new Graphics(); // 报错

//        Circle c = new Circle();

        // 在向下转型前，为了确保可以强制类型转换我们先使用 instanceof 进行判断. instanceof 在有继承关系的类之间使用
        if (graphics instanceof Triangle){
            Triangle t = (Triangle) graphics;
        } else if (graphics instanceof Circle){
            Circle c = (Circle) graphics;
        }
        triangle.draw();
        triangle.setLine();

        // JDK 16
        if (graphics instanceof Triangle t){
            t.setLine();
        }

        /*
            Person

            Dog

            Pig


            public void show(Object obj){
                Dog dog = (Dog) obj;
            }
         */

    }

    /**
     * 父类能出现的地方，子类一定能出现
     * @param obj
     * @return
     */
    public static Graphics print(Object obj){
        return new Triangle();
    }

}
